package com.zycfc.zsf.boot.util.collection;

import java.util.*;
import java.lang.reflect.*;
import com.google.common.collect.*;
import com.google.common.primitives.*;
import com.zycfc.zsf.boot.util.text.*;

public abstract class ArrayUtils
{
    private static final String ARRAY_PARAM = "\u5165\u53c2array";
    
    public static <T> T[] shuffle(final T[] array) {
        final List<T> list = new ArrayList<T>(array.length);
        Collections.addAll(list, array);
        Collections.shuffle(list);
        return list.toArray(array);
    }
    
    public static <T> T[] shuffle(final T[] array, final Random random) {
        final List<T> list = new ArrayList<T>((Collection<? extends T>)Arrays.asList(array));
        Collections.shuffle(list, random);
        return list.toArray(array);
    }
    
    public static <T> T[] concat(final T element, final T[] array) {
        return (T[])ObjectArrays.concat((Object)element, (Object[])array);
    }
    
    public static <T> T[] newArray(final Class<T> type, final int length) {
        return (T[])Array.newInstance(type, length);
    }
    
    public static <T> T[] toArray(final List<T> list, final Class<T> type) {
        return list.toArray((T[])Array.newInstance(type, list.size()));
    }
    
    public static <T> List<T> asList(final T... args) {
        return Arrays.asList(args);
    }
    
    public static <E> List<E> asList(final E first, final E[] rest) {
        return (List<E>)Lists.asList((Object)first, (Object[])rest);
    }
    
    public static List<Integer> intAsList(final int... backingArray) {
        return (List<Integer>)Ints.asList(backingArray);
    }
    
    public static List<Long> longAsList(final long... backingArray) {
        return (List<Long>)Longs.asList(backingArray);
    }
    
    public static List<Double> doubleAsList(final double... backingArray) {
        return (List<Double>)Doubles.asList(backingArray);
    }
    
    public static boolean isEmpty(final Object[] array) {
        return array == null || array.length == 0;
    }
    
    public static boolean isNotEmpty(final Object[] array) {
        return !isEmpty(array);
    }
    
    public static String cocat(final Object[] array, final String splitStr) {
        final StringBuffer sb = new StringBuffer();
        for (final Object str : array) {
            if (sb.length() > 0) {
                sb.append(splitStr);
            }
            sb.append(str.toString());
        }
        return sb.toString();
    }
    
    public static <T> List<T> toArrayList(final T[] array) {
        CheckUtil.notEmpty(array, "\u5165\u53c2array");
        final List<T> list = new ArrayList<T>();
        for (final T t : array) {
            list.add(t);
        }
        return list;
    }
}
